package com.ruoyi.bom.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bom.domain.TqlStapBase;
import com.ruoyi.bom.service.ITqlStapBaseService;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.bom.domain.TqlStapPrice;
import com.ruoyi.bom.service.ITqlStapPriceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 物料价格Controller
 * 
 * @author ruoyi
 * @date 2023-12-31
 */
@RestController
@RequestMapping("/price")
public class TqlStapPriceController extends BaseController
{
    @Autowired
    private ITqlStapPriceService tqlStapPriceService;

    /**
     * 查询物料价格列表
     */
    @RequiresPermissions("bom:price:list")
    @GetMapping("/list")
    public TableDataInfo list(TqlStapPrice tqlStapPrice)
    {
//        SecurityUtils.isAdmin(SecurityUtils.getUserId());
//        //价格跟门店相关联，如果是admin账号，不看价格
//        String deptCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
        startPage();
        List<TqlStapPrice> list = tqlStapPriceService.selectTqlStapPriceList(tqlStapPrice);
        return getDataTable(list);
    }


    @Autowired
    private ITqlStapBaseService tqlStapBaseService;

    /**
     * 查询物料价格列表
     */
//    @RequiresPermissions("bom:price:findPriceByStapCode")
    @GetMapping("/findPriceByStapCode")
    public AjaxResult findPriceByStapCode(String stapCode,String deptCode)
    {
        TqlStapPrice dto=new TqlStapPrice();
        dto.setStapCode(stapCode);
        dto.setDeptCode(deptCode);
        List<TqlStapPrice> list = tqlStapPriceService.selectTqlStapPriceList(dto);
        if(CollectionUtils.isEmpty(list))
        return success();
        else
        {
            TqlStapPrice  stapPrice=list.get(0);
            return  success(stapPrice);
        }
//        if(CollectionUtils.isEmpty(list)){
//            TqlStapBase tqlStapBase = tqlStapBaseService.getStapBaseByStapCode(stapCode);
//            tqlStapPriceService.insertTqlStapPriceByTqlStapBase(tqlStapBase);
//            TqlStapPrice stapPrice1 = tqlStapPriceService.findPriceByStapCode(stapCode);
//            return success(stapPrice1);
//        }
//        else {
//            TqlStapPrice  stapPrice=list.get(0);
//            return success(stapPrice);
//        }
    }

    /**
     * 修改设置价
     */
//    @RequiresPermissions("bom:price:updateInitPrice")
    @PostMapping("/updateInitPrice")
    public AjaxResult updateInitPrice(@RequestBody TqlStapPrice tqlStapPrice)
    {
        List<TqlStapPrice> list=tqlStapPriceService.selectTqlStapPriceList(tqlStapPrice);
        if(CollectionUtils.isEmpty(list))
        {
            TqlStapBase base= tqlStapBaseService.getStapBaseByStapCode(tqlStapPrice.getStapCode());
            tqlStapPrice.setStapId(base.getId()+"");
            tqlStapPrice.setStapName(base.getStapName());
            tqlStapPrice.setFlag("3");
            tqlStapPrice.setCreatedBy(SecurityUtils.getUsername());
            tqlStapPriceService.insertTqlStapPriceForSettingPrice(tqlStapPrice);
            return success();
        }
       else
        {
            tqlStapPrice.setId(list.get(0).getId());
            tqlStapPrice.setWeightedPrice(tqlStapPrice.getInitPrice());
            return success(tqlStapPriceService.updateInitPrice(tqlStapPrice));
        }
    }

    /**
     * 导出物料价格列表
     */
    @RequiresPermissions("bom:price:export")
    @Log(title = "物料价格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TqlStapPrice tqlStapPrice)
    {
        List<TqlStapPrice> list = tqlStapPriceService.selectTqlStapPriceList(tqlStapPrice);
        ExcelUtil<TqlStapPrice> util = new ExcelUtil<TqlStapPrice>(TqlStapPrice.class);
        util.exportExcel(response, list, "物料价格数据");
    }

    /**
     * 获取物料价格详细信息
     */
//    @RequiresPermissions("bom:price:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(tqlStapPriceService.selectTqlStapPriceById(id));
    }

    /**
     * 新增物料价格
     */
    @RequiresPermissions("bom:price:add")
    @Log(title = "物料价格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TqlStapPrice tqlStapPrice)
    {
        return toAjax(tqlStapPriceService.insertTqlStapPrice(tqlStapPrice));
    }


    /**
     * 修改物料价格
     */
    @RequiresPermissions("bom:price:edit")
    @Log(title = "物料价格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TqlStapPrice tqlStapPrice)
    {
        return toAjax(tqlStapPriceService.updateTqlStapPrice(tqlStapPrice));
    }

    /**
     * 删除物料价格
     */
    @RequiresPermissions("bom:price:remove")
    @Log(title = "物料价格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tqlStapPriceService.deleteTqlStapPriceByIds(ids));
    }




}
